<?php
/**
 * (actions) 提货申请（通过）
 */
namespace App\Admin\Actions\Shipping;

use App\Models\Shipping\SSkuApply;
use App\Models\Shipping\SSkuLog;
use Encore\Admin\Actions\BatchAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use App\Jobs\Shipping\SkuApproval;
class SkuBatchApproval extends BatchAction
{
    public $name = '通过';

    public function handle(Collection $collection)
    {
        try{
            $count = 0;
            $shippingDate = SSkuApply::initShippingDate();
            $sDate = array_key_first($shippingDate);
            $eDate = array_key_last($shippingDate);
            $items = [];
            $userId = Admin::user()->id;
            foreach ($collection as $model) {
                if($model->status != 2){ //待审批
                    continue;
                }
                DB::beginTransaction();
                $model->status = 4; //通过
                $flag = $model->save();
                if(!$flag){
                    DB::rollBack();
                    continue;
                }
                //提货申请
                $sca = SSkuApply::query()->where('area_id',$model->area_id)
                    ->where('psku_id',$model->psku_id)
                    ->where('shipping_date','>=',$sDate)
                    ->where('shipping_date','<=',$eDate)
                    ->get();
                $flag1 = false;
                $apply = [];
                foreach($sca as $_sca){
                    $_sca->approval_num = $_sca->apply_num;
                    $_sca->last_apply_num = $_sca->apply_num;
                    $flag2 = $_sca->save();
                    if(!$flag2){
                        $flag1 = false;
                        break;
                    }
                    $flag1 = true;
                    $apply[$_sca->shipping_date] = $_sca->approval_num;
                }
                if(!$flag1){
                    DB::rollBack();
                    continue;
                }
                //提货记录
                $flag3 = SSkuLog::query()->create([
                    'area_id' => $model->area_id,
                    'psku_id' => $model->psku_id,
                    'apply' => $apply,
                    'seller_id' => $model->seller_id,
                    'audit_id' => $userId
                ]);
                if($flag3){
                    $count++;
                    if(!in_array($model->psku_id,$items)){
                        $items[] = $model->psku_id;
                    }
                    DB::commit();
                }else{
                    DB::rollBack();
                }
            }

            //提货汇总
            if(!empty($items)){
                SkuApproval::dispatch($items,$shippingDate);
            }
            return $this->response()->success("成功通过{$count}条记录！")->refresh();
        }catch (\Exception $e){
            report($e);
            return $this->response()->error('系统异常！');
        }
    }
}
